﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PMPUtils.DB;

namespace PMPUtils.Maintenance
{
    public class UserMaintain
    {
        public static bool IsUserAlreadyExist(String userName)
        {
            String strQuery = "select count(id) exist from usr where usr_name = '" + userName + "'";
            DBOperator localOperator = new DBOperator(DBConfigurations.LOCAL_CONNECTION_STRING);
            DataTable dtExists = localOperator.Read(strQuery);

            bool isExists = false;

            foreach (DataRow row in dtExists.Rows)
            {
                if (row["exist"] != DBNull.Value)
                {
                    if (Convert.ToInt32(row["exist"]) >= 1)
                    {
                        isExists = true;
                    }
                }
            }

            return isExists;
        }

        public static void AddNewUser(String userName, String email,
            String team, String manager)
        {
            String strInsert = "insert into usr (usr_name, team, email, mgr_id, password) values (" +
                "'" + userName + "', '" + team + "', '" + email + "', " + manager + ", '')";

            DBOperator localOperator = new DBOperator(DBConfigurations.LOCAL_CONNECTION_STRING);
            localOperator.Update(strInsert);
        }
    }
}
